<script setup lang="ts">
import PatientItem from './components/PatientItem.vue'
import addPatient from './components/addPatient.vue'
import { getPatientInfoAPI } from '@/services/user'
import { onMounted, ref } from 'vue'
import type { PatientInfo } from '@/types/user'
import { useRoute, useRouter } from 'vue-router'
import { useConsultStore } from '@/stores'
// 定义患者列表数组
const patientList = ref<PatientInfo[]>([])
// 获取患者信息列表
const getPatientList = async () => {
  const res = await getPatientInfoAPI()
  patientList.value = res.data
  // 获取默认的患者id
  const defaultPatient = patientList.value.filter(
    (item) => item.defaultFlag === 1
  )
  if (defaultPatient[0].id) {
    selectPatientId.value = defaultPatient[0].id
  }
}
onMounted(async () => {
  getPatientList()
})

// 初始化患者信息
const initPatient: PatientInfo = {
  name: '',
  idCard: '',
  defaultFlag: 0,
  gender: 1
}
const patientInfo = ref<PatientInfo>({ ...initPatient })
const showPop = ref(false) // 弹出层的显示与隐藏
const popTitle = ref('') // 弹出层的标题
//添加|编辑患者
const togglePop = (info?: PatientInfo) => {
  if (info) {
    // 编辑患者
    popTitle.value = '编辑患者'
    // 将当前患者的信息进行复写
    patientInfo.value = info
  } else {
    // 添加患者
    popTitle.value = '添加患者'
    patientInfo.value = { ...initPatient }
  }
  showPop.value = true
}
// 保存、删除成功之后的操作
const successSave = () => {
  // 重新获取患者信息列表
  getPatientList()
  // 关闭弹出层
  showPop.value = false
}
// 判断是从那一条路径进入到该页面的
const route = useRoute()
const isChange = Boolean(route.query?.isChange)
const active = ref(0)
// 点击选择患者信息 默认选择默认患者

let selectPatientId = ref('')
const selectPatient = (item: PatientInfo, index: number) => {
  active.value = index
  selectPatientId.value = item.id as string
}
// 点击下一步，保存患者信息并跳转到支付页面
const store = useConsultStore()
const router = useRouter()
const next = () => {
  // 保存患者信息到pinia中
  store.setPatientInfo(selectPatientId.value)
  router.push({ name: 'PayConsult' })
}
</script>

<template>
  <div class="patient-page">
    <DP-NavBar
      :title="isChange ? '选择患者' : '家庭档案'"
      :back="
        () => {
          isChange
            ? router.push({ name: 'DepConsult' })
            : router.push({ name: 'user' })
        }
      "
    ></DP-NavBar>
    <div class="isChange" v-if="isChange">
      <h3>请选择患者信息</h3>
      <p>以便医生给出更准确的治疗，信息仅医生可见</p>
    </div>
    <div class="content">
      <patient-item
        :class="['patient-item', { selected: isChange && active === index }]"
        :patientInfo="item"
        v-for="(item, index) in patientList"
        :key="index"
        @editPatient="togglePop"
        @click="selectPatient(item, index)"
      ></patient-item>
      <div
        class="addPatient"
        v-if="patientList.length < 6"
        @click="togglePop()"
      >
        <van-icon name="plus" class="add" size="20" color="#16C2A3" />
        <p>添加患者</p>
      </div>
      <div class="tips">最多可添加<span class="num">6</span>人</div>
    </div>
    <div class="next" v-if="isChange">
      <van-button type="primary" block round @click="next">下一步</van-button>
    </div>
    <!-- 添加编辑患者弹出框 -->
    <van-popup
      v-if="showPop"
      v-model:show="showPop"
      position="right"
      :style="{ width: '100%', height: '100%' }"
    >
      <add-patient
        :title="popTitle"
        v-model:showPop="showPop"
        :patient-info="patientInfo"
        @success-save="successSave"
      ></add-patient>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.patient-page {
  margin-top: 46px;
  margin-bottom: 74px;
}
.content {
  padding: 15px;
  .selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    border: 1px solid var(--cp-primary);
  }
}
.addPatient {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 45px;
  padding: 15px 0;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-top: 15px;
  p {
    margin-top: 5px;
    color: var(--cp-primary);
  }
}
.tips {
  margin-top: 15px;
  color: var(--cp-tag);
  .num {
    margin: 0 3px;
  }
}
.isChange {
  padding: 15px;
  h3 {
    margin-bottom: 5px;
    font-weight: 500;
  }
  p {
    color: var(--cp-text3);
  }
}
.next {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 15px;
  background-color: #fff;
}
</style>
